// index.js
// 获取应用实例
const app = getApp();
const $get_api = require('../../common/api').HTTP;
const wx_auth = require('../../common/wx_auth') //微信授权js
const util = require('../../utils/util')
Page({

  data: {
    wx_auth:wx_auth.authApply,
    util:util,
    userInfo:{},
    activeIndex: 0,
    scrollHeight:null,
    isLoginBtn:true,
    daka_img_bg:app.imgUrl + "daka_img_bg.png"+ app.version,
    right_row:app.imgUrl + "right_row.png"+ app.version,
    activeList:[],
    index_bg:app.imgUrl + "index_bg.jpg"+ app.version,
    swiperCurrent: 0, //当前导航点
    page: 0, // page：当前页--
    img_h:app.imgUrl +"index/img_h.png"+ app.version,
    tabList: [
        { id:1,img:app.imgUrl + "index/icon1.png"+ app.version,text:"协会介绍"}, 
        { id:2,img:app.imgUrl + "index/icon2.png"+ app.version,text:"申请入会"},
        { id:3,img:app.imgUrl + "index/icon3.png"+ app.version,text:"我是会员"},
        { id:4,img:app.imgUrl + "index/icon4.png"+ app.version,text:"协会荣誉"},
        { id:5,img:app.imgUrl + "index/icon5.png"+ app.version,text:"友好协会"},
        { id:6,img:app.imgUrl + "index/icon6.png"+ app.version,text:"分会介绍"},
    ],
    modalHidden:true, //显示弹窗变量
    new_options:'',
    label_img:app.imgUrl + "my_qiye/label_img.png"+ app.version,
      right_icon:app.imgUrl + "youjiantou.png"+ app.version,
      img:app.imgUrl + "conpany_search/company_icon.png"+ app.version,
      keyword:'',// 搜索框内容，使用的是简易的双向绑定 //温习一下：第一层才能进行双向绑定
      keywords:[],//历史搜索的数组
      check_val:true,

  },
  onReady(){
    
  },
  onLoad() {

  },
  onShow(options){
    console.log(wx.getStorageSync('token'),"token");
    let _this = this;
    _this.data.new_options = options
    // let user_info = JSON.parse(wx.getStorageSync('userInfo'))
    if(wx.getStorageSync('token') == ""){
      _this.data.modalHidden = false
    }else{
      _this.data.modalHidden  = true
      _this.data.userInfo  = JSON.parse(wx.getStorageSync('userInfo'))
    }
    if(wx_auth.checkToken(1)){
      _this.data.isLoginBtn = false;
      _this.bannerList(1)
      _this.navShow();
      _this.setListHeight();
    }
    _this.setData({
        isLoginBtn: _this.data.isLoginBtn,
        modalHidden: _this.data.modalHidden ,
        // userInfo:JSON.parse(wx.getStorageSync('userInfo'))
    })
    console.log(_this.data.userInfo)
    console.log(_this.data.userInfo.user_type)
    
    
  },
   /**
     * 用户点击右上角分享
   */
  onShareAppMessage() {
    let _this = this;
    // 构建页面参数
    return util.wx_share('','','','',{})
  },
  /**
     * 授权
     */
  getUserProfile: function (options) {
        let that = this;
        // wx_auth.authApply(options,this.data.page,app.useConst.LOGO_CONFIG.WX_LOGIN_TYPE);
        wx_auth.authApply(options,'');
  },
  navShow:function(){
      //控制导航栏出现
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
            selected: 0
        })
    }
  },
  setListHeight() {
    let _this = this;
    wx.getSystemInfo({
      success: res => {
        _this.setData({
          scrollHeight: res.windowHeight - 48,
        });
      }
    });
  },
  receiveMessage:function(e){
    //接收H5传过来的数据
    let data = e.detail;
    console.log(data,"h5h5h5");
  },
  
  //图片变化时
  swiperChange: function (e) {
      this.setData({
          swiperCurrent: e.detail.current
      })
  },
  goto_huodong_page:function(e){
    console.log(e,"11");
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../my_shop_dec/my_shop_dec?id='+id,
    })
  },
    gotopage:function(e){
        let u_page = e.currentTarget.dataset.upage;
        let that=this;
        // console.log(u_page,"u_page");
        if(u_page == 1){
            wx.navigateTo({
              url: '../my_company_introduce/my_company_introduce?id='+u_page,
            })
        }
        else if(u_page == 2){
            wx.navigateTo({
              url: '../ruhui/ruhui?id='+u_page,
            })
        }
        //我是会员
        else if(u_page == 3){
          if(that.data.userInfo.user_type==1)
          {
            console.log("是会员")
            wx.showToast({
              title: '您已是会员',
            })
          }
          else
          {
            wx.navigateTo({
              url: '../apply_member_join/apply_member_join?id='+u_page,
            })
          }
         
      }
      //协会荣誉
      else if(u_page == 4){
        // wx.navigateTo({
        //   url: '../rongyu/rongyu?id='+u_page,
        // })
     }
      //友好协会
      // else if(u_page == 5){
      //     wx.navigateTo({
      //       url: '../ruhui/ruhui?id='+u_page,
      //     })
      // }
      //分会介绍
      //   else if(u_page == 6){
      //     wx.navigateTo({
      //       url: '../ruhui/ruhui?id='+u_page,
      //     })
      // }
    },
    bannerList:function(type = 2){
        let that = this;
        $get_api.getActivityList({ 
            page:1,
            limit:10
        }).then(r => { 
        let data = r.data.data;
        console.log(data,"获取banner成功");
        that.data.activeList = [];
        that.data.activeList = data.info;
        that.setData({
            activeList:that.data.activeList
        })
      })
    },
    /**
   * 显示弹窗
   */
  show_wx: function() {
    this.setData({
      modalHidden: false
    })
  },
  
  /**
   *  点击确认
   */
  modalConfirm: function() {
    // do something
    this.setData({
      modalHidden: true
    })
  },
  check_token:function(e){
    console.log(e,"111");
    let _this = this;
    let index = e.currentTarget.dataset.index;
    if(index == 1){
      //游客登录
      wx_auth.authApply(_this.data.new_options,'','',{},0);
    }
    else if(index == 2){
      //会员登录
      let obj = {jump_page:"../apply_member_join/apply_member_join"}
      wx_auth.authApply(_this.data.new_options,'','',obj,0);
    }
     

  },
  //注册的checkbox
  zhengceCheck:function(e){
    this.data.check_val = e.detail.value
    this.setData({
      check_val:e.detail.value
    })
  },
  //查看隐私政策
  gotoZhengce:function(){
      wx.navigateTo({
        url: '../zhengce/zhengce',
      })
  },
  //点击搜索按钮
  onSearch(){
    let that = this
    // this.data.keyword获取数据，
    // if(keyword == ''){
    //   return wx.showToast({
    //     icon:'none',
    //     title: '请输入搜索内容',
    //   })
    // }
    //trim去除字符串前后多余空格
    const keyword=this.data.keyword.trim()
    that.data.keyword = keyword
   //增加数组长度的步骤也可以使用展开运算符
   const arr=[keyword,...this.data.keywords]
   
    this.setData({
        //先转化为set去重
        //然后重新转化为数组
        keywords:Array.from(new Set(arr)),
        //搜索完成清空搜索框
        // keyword:''
    })
    // that.shopList(1,that.data.limit); //调取接口的 位置
    wx.navigateTo({
      url: '../company_search/company_search?cate_id='+0+'&keyword='+keyword+'&data_title=全部',
    })
    console.log(this.data.keyword)
},
  
})

